//
//  SubUserTableViewCell.m
//  CarTown
//
//  Created by 熊浩 on 15/9/3.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "SubUserTableViewCell.h"
#import "SubUserMode.h"
#import "Puser.h"
#import "UserInfoUpdateResde.h"
#define kTableTopBorderWidth 8
@interface SubUserTableViewCell ()
@property (weak, nonatomic) IBOutlet UILabel *username;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *job;
@property (weak, nonatomic) IBOutlet UILabel *phone;
@property (weak, nonatomic) IBOutlet UIView *disdview;
@property (weak, nonatomic) IBOutlet UIButton *leftbutton;
@property (weak, nonatomic) IBOutlet UIButton *editbutton;
@property (nonatomic,strong) Puser *user;
@end


@implementation SubUserTableViewCell
-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}
- (void)awakeFromNib {
    // Initialization code
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:self.bounds];
    [shapeLayer setPosition:self.center];
    [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
    
    // 设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:[[UIColor blackColor] CGColor]];
    [shapeLayer setStrokeColor:[[UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1.0f] CGColor]];
    
    // 3.0f设置虚线的宽度
    [shapeLayer setLineWidth:1.0f];
    [shapeLayer setLineJoin:kCALineJoinRound];
    
    // 3=线的宽度 1=每条线的间距
    [shapeLayer setLineDashPattern:
     [NSArray arrayWithObjects:[NSNumber numberWithInt:3],
      [NSNumber numberWithInt:1],nil]];
    
    // Setup the path
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL, XHScreenwidth,0);
    
  
    
    [shapeLayer setPath:path];
    CGPathRelease(path);
    
    // 可以把self改成任何你想要的UIView, 下图演示就是放到UITableViewCell中的
    [[self.disdview layer] addSublayer:shapeLayer];
    self.layer.borderColor=[BoderColor CGColor];
    self.layer.borderWidth=0.5;
}

+(instancetype)cellWithTableView:(UITableView *)tableview{
    SubUserTableViewCell *cell=[tableview dequeueReusableCellWithIdentifier:@"sub"];
    if (cell==nil) {
        cell=[[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil]lastObject];
    
    }
    return cell;
}

- (IBAction)leftclick:(UIButton *)sender {
    sender.selected=!sender.isSelected;
    if (sender.isSelected) {
        self.username.textColor=[UIColor lightGrayColor];
         self.name.textColor=[UIColor lightGrayColor];
         self.job.textColor=[UIColor lightGrayColor];
         self.phone.textColor=[UIColor lightGrayColor];
    }else{
        self.username.textColor=[UIColor darkGrayColor];
        self.name.textColor=[UIColor darkGrayColor];
        self.job.textColor=[UIColor darkGrayColor];
        self.phone.textColor=[UIColor darkGrayColor];
    }
    [self lockuser:sender.isSelected];
}
-(void)lockuser:(BOOL)islock{
    if ([_delegate respondsToSelector:@selector(updateState:)]) {
        [_delegate updateState:STATESTART];
    }
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    params[@"action"]=@"c_child_ban";
    params[@"child_id"]=_mode.com_id;
    params[@"user_type"]=@"company";
    params[@"com_id"]=self.user.com_id;
    if (islock) {
        params[@"ban_type"]=@"ban";
    }else{
        params[@"ban_type"]=@"unban";
    }
    [PostUitls Post:userinfourl parameters:params success:^(id response) {
       
        UserInfoUpdateResde *resmode=[UserInfoUpdateResde resdeWithDict:response];
        if ([resmode.state integerValue]==400) {
            if ([_delegate respondsToSelector:@selector(updateState:)]) {
                [_delegate updateState:STATESTOP];
            }
        }else{
            if ([_delegate respondsToSelector:@selector(updateState:)]) {
                [_delegate updateState:STATEERROR];
            }
        }
        
    } failure:^(NSError *error) {
        if ([_delegate respondsToSelector:@selector(updateState:)]) {
            [_delegate updateState:STATEERROR];
        }
    }];
}
- (IBAction)bianjiclick:(id)sender {
    if ([_delegate respondsToSelector:@selector(bianBtnClick:)]) {
        [_delegate bianBtnClick:self];
    }
    
}

-(void)setMode:(SubUserMode *)mode{
    _mode=mode;
    self.username.text=_mode.com_username;
    self.name.text=_mode.com_ctusername;
    self.job.text=_mode.com_job;
    self.phone.text=_mode.com_ctphone;
    if ([_mode.locked integerValue]==0) {
        self.leftbutton.selected=NO;
        self.username.textColor=[UIColor darkGrayColor];
        self.name.textColor=[UIColor darkGrayColor];
        self.job.textColor=[UIColor darkGrayColor];
        self.phone.textColor=[UIColor darkGrayColor];
    }else{
        self.leftbutton.selected=YES;
        self.username.textColor=[UIColor lightGrayColor];
        self.name.textColor=[UIColor lightGrayColor];
        self.job.textColor=[UIColor lightGrayColor];
        self.phone.textColor=[UIColor lightGrayColor];
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setFrame:(CGRect)frame{
    frame.origin.y += kTableTopBorderWidth;
    frame.size.height -= kTableTopBorderWidth;
    [super setFrame:frame];
}

@end
